# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Celeste <cielesti@protonmail.com>
pkgname=ocaml-camomile
_pkgname=camomile
pkgver=1.0.2
pkgrel=3
pkgdesc="A Unicode library for OCaml"
url="https://github.com/yoriyuki/Camomile"
arch="all !riscv64 !loongarch64" # ocaml
license="LGPL-2.0-or-later"
depends="$pkgname-data=$pkgver-r$pkgrel"
depends_dev="$pkgname=$pkgver-r$pkgrel"
makedepends="dune ocaml ocaml-compiler-libs ocaml-cppo ocaml-findlib"
subpackages="$pkgname-dev $pkgname-data::noarch"
options="!check"
source="$pkgname-$pkgver.tar.bz2::https://github.com/yoriyuki/Camomile/releases/download/$pkgver/camomile-$pkgver.tbz"
builddir="$srcdir/$_pkgname-$pkgver"

# 32-bit archs
case "$CARCH" in
	arm*|x86) options="$options textrels" ;;
esac

build() {
	# Workaround for https://github.com/yoriyuki/Camomile/issues/39.
	if [ "$CARCH" = ppc64le ]; then
		ulimit -Hs unlimited
		ulimit -Ss 65536
	fi

	export OCAMLPATH=/usr/lib/ocaml
	dune build -p camomile @install
}

check() {
	dune runtest
}

package() {
	dune install \
		--destdir="$pkgdir" \
		--prefix=/usr \
		--libdir=/usr/lib/ocaml \
		camomile

	# There's just a readme and changelog.
	rm -Rf "$pkgdir"/usr/doc
}

dev() {
	default_dev

	cd "$pkgdir"

	local path; find usr/lib/ocaml \( \
			-name '*.cmt' -o \
			-name '*.cmti' -o \
			-name '*.ml' -o \
			-name '*.mli' \
		\) | while read -r path
	do
		amove "$path"
	done
}

data() {
	pkgdesc="$pkgdesc (data files)"
	depends=""

	amove usr/share/"$_pkgname"
}

prepare_py_provides() {
	local datadir="${subpkgdir:-$pkgdir}"
	local pkgbasedir=${pkgbasedir:-"$startdir/pkg"}
	local controldir="$pkgbasedir"/.control.${subpkgname:-$pkgname}

	options_has "!tracedeps" && return 0

	ocaml4-abuild-find provides \
		"$datadir"/usr/lib/ocaml \
		"$controldir" \
		"$pkgver-r$pkgrel"
}

scan_python3_dependency() {
	local controldir="$2" datadir="$3"

	ocaml4-abuild-find requires \
		"$datadir"/usr/lib/ocaml \
		"$controldir"
}

sha512sums="
7586422e68779476206027c6ebbe19b677fbe459153221f7c952c7fae374c5c8232249cb76fdb1f482069707aa1580be827cd39693906142988268b7f0e7f6d0  ocaml-camomile-1.0.2.tar.bz2
"
